package com.jusfoun.onlinecommunicationclient.controller;

import com.jusfoun.onlinecommunicationclient.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 主页显示
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Value("${websocket-server-url}")
    private String socketServerUrl;

    @Value("${websocket-server-appId}")
    private String appId;

    @Value("${websocket-server-appSecret}")
    private String appSecret;

    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("socketServerUrl", socketServerUrl);
        return "index2";
    }


    @RequestMapping(value = "/token", method = RequestMethod.POST)
    @ResponseBody
    public String getToken(HttpServletRequest request, String userName) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("appId", appId);
        map.put("appSecret", appSecret);
        map.put("userName", userName);
        return HttpClientUtil.sendHttpPost(socketServerUrl + "/user/generate-token", map);
    }
}
